package com.xukai.springboot.datalayer.statis;

import com.xukai.springboot.entity.statistics.t_statis_scene;
import com.xukai.springboot.entity.statistics.t_statis_scene_chart;
import com.xukai.springboot.entity.statistics.t_statis_scene_theme;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StatisSceneMapper {

    /**
     * 获取场景列表
     */
    @SelectProvider(type = StatisSceneMapperProvider.class, method = "GetSceneList")
    public List<t_statis_scene> GetSceneList(@Param("name") String name, @Param("state") int state, @Param("start") int start, @Param("pageSize") int pageSize);

    /**
     * 获取场景详细信息
     */
    @Select(" select id ,sceneName,`describe`,state,themeID,themeName,grid from t_statis_scene where id=#{id}")
    public t_statis_scene GetScene(@Param("id") int id);

    /**
     * 新建场景基本信息
     */
    @Insert("INSERT INTO t_statis_scene ( sceneName,`describe`,state,themeID,themeName,createTime,grid )\n" +
            "VALUES (#{sceneName},#{describe},#{state},#{themeID},#{themeName},#{createTime},#{grid})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddSceneBase(t_statis_scene data);

    /**
     * 修改场景基本信息
     */
    @Update(" update t_statis_scene set sceneName=#{sceneName},`describe`=#{describe},state=#{state},themeID=#{themeID},themeName=#{themeName} ,grid=#{grid} where id=#{id}")
    public void UpdateSceneBase(t_statis_scene data);


    /**
     * 增加图表
     */
    @Insert(" INSERT INTO t_statis_scene_chart (sceneID,sort,chartID,width,height)\n" +
            " VALUES (#{sceneID},#{sort},#{chartID},#{width},#{height})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddChart(t_statis_scene_chart chartData);

    /**
     * 获取图表列表
     */
    @SelectProvider(type = StatisSceneMapperProvider.class, method = "GetChartList")
    public List<t_statis_scene_chart> GetChartList(@Param("sceneID") int sceneID, @Param("start") int start, @Param("pageSize") int pageSize);


    /**
     * 删除图表
     */
    @Delete(" DELETE from  t_statis_scene_chart where id=#{id}")
    public void DelChart(@Param("id") int id);

    /**
     * 删除场景
     */
    @Delete(" DELETE from  t_statis_scene  where id=#{id}")
    public void DelScene(@Param("id") int id);

    /**
     * 根据场景ID删除图表
     *
     * @param sceneID
     */
    @Delete(" Delete from t_statis_scene_chart where sceneID=#{sceneID}")
    public void DelChartBySceneID(@Param("sceneID") int sceneID);

    /**
     * 获取场景数据总数
     *
     * @param name
     * @param state
     * @return
     */
    @SelectProvider(type = StatisSceneMapperProvider.class, method = "GetSceneCount")
    @ResultType(Integer.class)
    public int GetSceneCount(@Param("name") String name, @Param("state") int state);

    /**
     * 获取图表数据总数
     *
     * @return
     */
    @SelectProvider(type = StatisSceneMapperProvider.class, method = "GetChartCount")
    @ResultType(Integer.class)
    public int GetChartCount(@Param("id") int id);


    /**
     * 获取主题列表
     */
    @Select("select id,themeCode,themeName from t_statis_scene_theme order by themeName")
    public List<t_statis_scene_theme> GetThemeList();

    /**
     * 获取主题详细信息
     *
     * @param id
     * @return
     */
    @Select("select id,themeCode,themeName,backgroundColor,color from t_statis_scene_theme where id=#{id}")
    public t_statis_scene_theme GetTheme(@Param("id") int id);

    /**
     * 判断图表是否已经存在
     *
     * @param sceneID
     * @param chartID
     * @return
     */
    @Select("select count(id) as total from t_statis_scene_chart where sceneID=#{sceneID} and chartID=#{chartID}")
    @ResultType(Integer.class)
    public int ChartIsExist(@Param("sceneID") int sceneID, @Param("chartID") int chartID);

    /**
     * 获取最大排序序号
     *
     * @param sceneID
     * @return
     */
    @Select("select IFNULL(MAX(sort),0)  as sort from t_statis_scene_chart  where sceneID=#{sceneID}")
    @ResultType(Integer.class)
    public int GetMaxSort(@Param("sceneID") int sceneID);

    /**
     * 修改排序
     */
    @Update("UPDATE t_statis_scene_chart set sort= #{sort} ,width=#{width},height=#{height} where sceneID=#{sceneID} and chartID=#{chartID}")
    public void UpdateChartSort(@Param("sceneID") int sceneID,@Param("width") String width,@Param("height") String height, @Param("chartID") int chartID, @Param("sort") int sort);

    /**
     *
     */
    class StatisSceneMapperProvider {

        /**
         * 获取场景列表
         *
         * @param name
         * @param start
         * @param pageSize
         * @return
         */
        public String GetSceneList(String name, int state, int start, int pageSize) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id ,sceneName,`describe`,state,themeID,themeName ,grid from t_statis_scene ");
            sb.append(" where 1=1");
            if (!name.isEmpty()) {
                sb.append(" and dataSourceName like concat(concat('%',#{name}),'%') ");
            }
            if (state > -1) {
                sb.append(" and state=#{state}");
            }
            sb.append(" order by createTime desc");
            if (pageSize > -1) {
                sb.append(" limit #{start},#{pageSize}");
            }
            return sb.toString();
        }

        /**
         * 获取场景数据总数
         *
         * @param name
         * @param state
         * @return
         */
        public String GetSceneCount(String name, int state) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count( id) as total from t_statis_scene ");
            sb.append(" where 1=1");
            if (state > -1) {
                sb.append(" and state=#{state}");
            }
            return sb.toString();
        }


        /**
         * 获取图表列表
         *
         * @param id
         * @param start
         * @param pageSize
         * @return
         */
        public String GetChartList(int sceneID, int start, int pageSize) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select a.id, a.sceneID, a.chartID, b.chartName,b.chartType as chartTypeID,c.`name` as chartTypeName ,a.sort, a.width,a.height from t_statis_scene_chart a ");
            sb.append(" join t_statis_chart_setting b on a.chartID=b.id");
            sb.append(" join t_sys_chart_type c on b.chartType=c.id");
            sb.append(" where a.sceneID=#{sceneID}");
            sb.append(" order by a.sort");
            if (pageSize > -1) {
                sb.append(" limit #{start},#{pageSize}");
            }
            return sb.toString();
        }

        /**
         * 获取图表数据总数
         *
         * @param id
         * @return
         */
        public String GetChartCount(int id) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count(id) as total from t_statis_scene_chart  ");
            sb.append(" where sceneID=#{id}");
            return sb.toString();
        }
    }

}
